package com.sky.controller.user;

import com.sky.constant.ShopStatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.*;
import com.sky.result.Result;
import com.sky.service.UserLoginService;
import com.sky.service.UserService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author iTaotao
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description 用户控制器
 * @createTime 2023年07月31日 16:26:00
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    /**
     * 获得状态
     *
     * @return {@link Result}<{@link Integer}>
     */
    @GetMapping("/shop/status")
    public Result<Integer> getStatus() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer status = (Integer) valueOperations.get(ShopStatusConstant.SHOP_STATUS);
        return Result.success(status);
    }


    /**
     * 选择类别列表
     *
     * @param categoryDTO 类别dto
     * @return {@link Result}<{@link List}<{@link Category}>>
     */
    @GetMapping("/category/list")
    public Result<List<Category>> selectCategoryList(CategoryDTO categoryDTO) {
        List<Category> categoryList = userService.selectCategoryList(categoryDTO);
        return Result.success(categoryList);

    }

    /**
     * 选择菜列表
     *
     * @param categoryId 类别id
     * @return {@link Result}<{@link List}<{@link Dish}>>
     */
    @GetMapping("/dish/list")
    public Result<List<DishVO>> selectDishList(Integer categoryId) {
        String key = "dish_" + categoryId;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishVO> dishVOList = (List<DishVO>) valueOperations.get(key);
        if(dishVOList != null && dishVOList.size() > 0){
            return Result.success(dishVOList);
        }

        dishVOList = userService.selectDishList(categoryId);
        valueOperations.set(key, dishVOList);
        return Result.success(dishVOList);
    }

    /**
     * 选择套餐列表
     *
     * @param categoryId 类别id
     * @return {@link Result}<{@link List}<{@link Setmeal}>>
     */
    @GetMapping("/setmeal/list")
    public Result<List<Setmeal>> selectSetMealList(Long categoryId) {


      List<Setmeal>  setmealList = userService.selectSetMealList(categoryId);

        return Result.success(setmealList);
    }

    /**
     * 选择套餐菜
     *
     * @param id id
     * @return {@link Result}<{@link List}<{@link SetmealDish}>>
     */
    @GetMapping("/setmeal/dish/{id}")
    public Result<List<DishItemVO>> selectSetMealDish(@PathVariable Long id) {
        List<DishItemVO> dishVO = userService.selectSetMealDish(id);
        return Result.success(dishVO);
    }

    /**
     * 选择商店购物车列表
     *
     * @return {@link Result}<{@link List}<{@link ShoppingCart}>>
     */

    @PostMapping("/shoppingCart/add")
    public Result<?> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        userService.add(shoppingCartDTO);
        return Result.success();

    }

    @GetMapping("/shoppingCart/list")
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> shoppingCart = userService.list();
        return Result.success(shoppingCart);
    }

    @DeleteMapping("/shoppingCart/clean")
    public Result<?> clean() {
        userService.clean();
        return Result.success();
    }

    @PostMapping("/shoppingCart/sub")
    public Result<?> sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        userService.sub(shoppingCartDTO);
        return Result.success();

    }


}
